﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Text;
using System.Data;
using System.Collections;

public partial class YSFGL_YSFGLList : System.Web.UI.Page
{
  protected void Page_Load(object sender, EventArgs e)
  {
    if (!Page.IsPostBack)
    {
      if (!ProjectPrivate.GetQX("50"))
      {
        Server.Transfer("~\\blank.aspx");
      }
      //if (CWSD.GetSDZT("否"))
      //{
      //  Server.Transfer("~\\sd.aspx");
      //}
      date1.Date = CommonUse.GetYC();
      date2.Date = CommonUse.GetYM();
    }
    BindGrid();
  }

  protected void BindGrid()
  {
    StringBuilder sbSql = new StringBuilder();
    sbSql.Append(@"select  ROW_NUMBER() OVER (ORDER BY a.cbsjid desc) AS xh,a.*,convert(varchar,a.CBSJ,23) BYCBSJ,c.KHMC,c.KH,C.XXDZ,D.YQXZ,e.XM,b.BQQL,b.yqxzid,b.bqql_sfsb,
B.JG,B.JJ,b.YSJG SR,B.YSJJ JGJJ,B.YSFY BQFY,b.ssql
FROM      sjlr_jgtz_cbsj A join sjlr_jgtz_bqql B on a.cbsjid=b.cbsjid 
            left join jcxx_khxx c on a.khid=c.khid 
            left join jcxx_yqxz d on D.yqxzid=b.yqxzid  
            left join sys_user e on e.dlm=a.dlm
    WHERE c.dwid in (select dwid from sys_yhdw where dlm=@2) ");
    if (ddlZT.SelectedValue == "0")
    {
      sbSql.Append("and a.cbsj>=@0 and a.cbsj<=cast(@1 as datetime) and bqql_sfsb=1");
    }
    if (ddlZT.SelectedValue == "1")
    {
      sbSql.Append("and b.shsj>=@0 and b.shsj<=cast(@1 as datetime) and bqql_sfsb=2");
    }
    //sbSql.Append("order by a.cbsjid desc");
    SqlParameter[] cmdParams = { new SqlParameter("@0", SqlDbType.VarChar, 10), new SqlParameter("@1", SqlDbType.VarChar, 10) ,
                               new SqlParameter("@2", SqlDbType.VarChar, 10)};
    cmdParams[0].Value = date1.Text;
    cmdParams[1].Value = date2.Text;
    cmdParams[2].Value = SysManage.Get_User;
    DataSet ds = DbHelperSQL.Query(sbSql.ToString(), cmdParams);
    ASPxGridView1.DataSource = ds;
    ASPxGridView1.DataBind();
  }

  protected void lbFH_Command(object sender, CommandEventArgs e)
  {
    if (!ProjectPrivate.GetQX("21"))
    {
      Server.Transfer("~\\blank.aspx");
    }
    //if (CBSJ.SFCH(e.CommandArgument.ToString()))
    //{
    //  CommonUse.Alert("已缴费，无法撤回！");
    //  return;
    //}
    string[] s = e.CommandArgument.ToString().Split(new char[] { '_' });
    if (CBSJ.ExistHQID(s[1], s[2]))
    {
      CommonUse.Alert("本次日期之后已存在数据，不能撤回");
      return;
    }
    string sKHID = s[1];
    SqlParameter[] cmdParams = null;
    string sSql = "";
    List<CommandInfo> cis = new List<CommandInfo>();
    CommandInfo ci;

    sSql = "update SJLR_bqql set bqql_SFSB=0 where cbsjid=" + s[0];
    ci = new CommandInfo();
    ci.CommandText = sSql;
    ci.Parameters = cmdParams;
    cis.Add(ci);

    if (DbHelperSQL.ExecuteSqlTran(cis) > 0)
    {
      ZHCX.CreateZHCX(sKHID, DateTime.Now.ToString("yyyy-MM-dd"));
      ZHCX.UpdateCB(sKHID, DateTime.Now.ToString("yyyy-MM-dd"), s[0]);
      ZHCX.UpdateJF(sKHID, DateTime.Now.ToString("yyyy-MM-dd"));
      string nr = "应收费撤回:" + "客户编码为" + s[1];
      rzcz.InsertRZ(nr);
      BindGrid();
    }
  }

  protected void lbDelete_Command(object sender, CommandEventArgs e)
  {
    string[] s = e.CommandArgument.ToString().Split(new char[] { ',' });
    DataTable dt = CBSJ.Get_JGTZ_BQQL(s[0], s[1]);
    //    double dSQJY = Convert.ToDouble(KHXX.GetYE(dt.Rows[0]["khid"].ToString()));
    //    double dJE = dSQJY < 0 ? 0 : dSQJY;//账户余额
    //    double dTemp = 0;

    SqlParameter[] cmdParams = null;
    string sSql = "";
    List<CommandInfo> cis = new List<CommandInfo>();
    CommandInfo ci;
    ArrayList arrValue = new ArrayList();
    ArrayList arrLengh = new ArrayList();
    #region
    //    string sJFSJID = CommonUse.GetID("sjlr_jfsj", "jfsjid");

    //    if (dJE > 0)
    //    {
    //      #region  修改本期气量表，插入缴费明细
    //      for (int i = 0; i < dt.Rows.Count; i++)
    //      {
    //        dJE = Math.Round(dJE + Convert.ToDouble(dt.Rows[i]["ssfy"].ToString()), 2);
    //        if (dJE >= 0)
    //        {
    //          sSql = "update sjlr_bqql set ssql=bqql,ssfy=ysfy,ssjj=ysjj,ssjg=ysjg where cbsjid=@0 and yqxzid=@1";
    //          cmdParams = new SqlParameter[2] { new SqlParameter("@0", SqlDbType.VarChar, 10), new SqlParameter("@1", SqlDbType.VarChar, 10) };
    //          cmdParams[0].Value = dt.Rows[i]["cbsjid"].ToString();
    //          cmdParams[1].Value = dt.Rows[i]["yqxzid"].ToString();
    //          ci = new CommandInfo();
    //          ci.CommandText = sSql;
    //          ci.Parameters = cmdParams;
    //          cis.Add(ci);

    //          arrValue = new ArrayList();
    //          arrValue.Add(sJFSJID);
    //          arrValue.Add(dt.Rows[i]["yqxz"].ToString());
    //          arrValue.Add(dt.Rows[i]["jg"].ToString());
    //          arrValue.Add(Convert.ToDouble(dt.Rows[i]["ssql"].ToString()) * (-1));
    //          arrValue.Add(dt.Rows[i]["jj"].ToString());
    //          arrValue.Add(Convert.ToDouble(dt.Rows[i]["ssfy"].ToString()) * (-1));
    //          arrValue.Add(dt.Rows[i]["cbsjid"].ToString());
    //          arrLengh = new ArrayList();
    //          arrLengh.Add("20");
    //          arrLengh.Add("20");
    //          arrLengh.Add("20");
    //          arrLengh.Add("20");
    //          arrLengh.Add("20");
    //          arrLengh.Add("20");
    //          arrLengh.Add("20");
    //          cis.Add(CommonUse.InsertCI("sjlr_jfmx", arrValue, arrLengh));
    //        }
    //        else
    //        {
    //          double fy = Math.Round(dJE - Convert.ToDouble(dt.Rows[i]["ssfy"].ToString()), 2);
    //          dTemp = Convert.ToDouble(dt.Rows[i]["jg"].ToString()) + Convert.ToDouble(dt.Rows[i]["jj"].ToString());
    //          double ql = Math.Floor(fy / dTemp * 1000) / 1000;
    //          sSql = @"update sjlr_bqql set ssql=ssql+{1},ssfy={0},ssjj=floor({0}/(jg+jj))*jj,
    //ssjg=floor({0}/(jg+jj))*jg where cbsjid=@0 and yqxzid=@1";
    //          cmdParams = new SqlParameter[2] { new SqlParameter("@0", SqlDbType.VarChar, 10), new SqlParameter("@1", SqlDbType.VarChar, 10) };
    //          cmdParams[0].Value = dt.Rows[i]["cbsjid"].ToString();
    //          cmdParams[1].Value = dt.Rows[i]["yqxzid"].ToString();
    //          ci = new CommandInfo();
    //          ci.CommandText = string.Format(sSql, dJE, Math.Round(ql, 3));
    //          ci.Parameters = cmdParams;
    //          cis.Add(ci);

    //          arrValue = new ArrayList();
    //          arrValue.Add(sJFSJID);
    //          arrValue.Add(dt.Rows[i]["yqxz"].ToString());
    //          arrValue.Add(dt.Rows[i]["jg"].ToString());
    //          arrValue.Add(Math.Round(ql, 3));
    //          arrValue.Add(dt.Rows[i]["jj"].ToString());
    //          arrValue.Add(fy);
    //          arrValue.Add(dt.Rows[i]["cbsjid"].ToString());
    //          arrLengh = new ArrayList();
    //          arrLengh.Add("20");
    //          arrLengh.Add("20");
    //          arrLengh.Add("20");
    //          arrLengh.Add("20");
    //          arrLengh.Add("20");
    //          arrLengh.Add("20");
    //          arrLengh.Add("20");
    //          cis.Add(CommonUse.InsertCI("sjlr_jfmx", arrValue, arrLengh));
    //        }
    //      }
    //      if (DbHelperSQL.ExecuteSqlTran(cis) > 0)
    //      {

    //      }
    //      #endregion
    //  #region  插入缴费数据
    //  double dBCJQK = Convert.ToDouble(CBSJ.GetBCJQK(dt.Rows[0]["khid"].ToString()));

    //  cis = new List<CommandInfo>();
    //  //cis.Add(CommonUse.DeleteCI("sjlr_jfsj","jfsjid",sID));

    //  arrValue = new ArrayList();
    //  arrValue.Add(sJFSJID);
    //  arrValue.Add(SysManage.Get_User);
    //  arrValue.Add(1);
    //  arrValue.Add(dt.Rows[0]["khid"].ToString());
    //  arrValue.Add(DateTime.Now.ToString());
    //  arrValue.Add("0");
    //  arrValue.Add("0");
    //  arrValue.Add(JFSJ.GetBH());
    //  arrValue.Add(dBCJQK);
    //  arrValue.Add(dJE < 0 ? dBCJQK.ToString() : dJE.ToString());
    //  arrValue.Add(Convert.ToDouble(dt.Rows[0]["ssfy"].ToString()));
    //  arrLengh = new ArrayList();
    //  arrLengh.Add("20");
    //  arrLengh.Add("20");
    //  arrLengh.Add("20");
    //  arrLengh.Add("20");
    //  arrLengh.Add("20");
    //  arrLengh.Add("20");
    //  arrLengh.Add("20");
    //  arrLengh.Add("20");
    //  arrLengh.Add("20");
    //  arrLengh.Add("20");
    //  arrLengh.Add("20");
    //  cis.Add(CommonUse.InsertCI("sjlr_jfsj", arrValue, arrLengh));
    //  #endregion

    //  sSql = "update jcxx_khxx set zhye=" + (dJE < 0 ? "0" : dJE.ToString()) + " where khid=" + dt.Rows[0]["khid"].ToString();
    //  cis.Add(new CommandInfo(sSql, null));
    //}
    #endregion

    sSql = "update SJLR_bqql set bqql_SFSB=2,shr='" + SysManage.Get_User
      + "',shsj='" + DateTime.Now.ToString("yyyy-MM-dd") + "' where cbsjid=" + s[0] + " and yqxzid=" + s[1];
    ci = new CommandInfo();
    ci.CommandText = sSql;
    ci.Parameters = cmdParams;
    cis.Add(ci);

    sSql = "update SJLR_jgtz_bqql set bqql_SFSB=2,shr='" + SysManage.Get_User
      + "',shsj='" + DateTime.Now.ToString("yyyy-MM-dd") + "' where cbsjid=" + s[0] + " and yqxzid=" + s[1];
    ci = new CommandInfo();
    ci.CommandText = sSql;
    ci.Parameters = cmdParams;
    cis.Add(ci);
    if (DbHelperSQL.ExecuteSqlTran(cis) > 0)
    {
      ZHCX.CreateZHCX(dt.Rows[0]["khid"].ToString(), DateTime.Now.ToString("yyyy-MM-dd"));
      ZHCX.UpdateCB_jgtz(dt.Rows[0]["khid"].ToString(), DateTime.Now.ToString("yyyy-MM-dd"), s[0]);
      //ZHCX.UpdateJF(dt.Rows[0]["khid"].ToString(), DateTime.Now.ToString("yyyy-MM-dd"));
      BindGrid();
    }
  }

  protected void lbSB_Command(object sender, CommandEventArgs e)
  {
    //if (CBSJ.SFCH(e.CommandArgument.ToString()))
    //{
    //  CommonUse.Alert("已缴费，无法撤回！");
    //  return;
    //}
    string sKHID = "", sRQ = "";
    SqlDataReader dr = CBSJ.GetCBSJ(e.CommandArgument.ToString());
    if (dr.Read())
    {
      sKHID = dr["khid"].ToString();
      sRQ = dr["rq"].ToString();
    }
    dr.Close();

    SqlParameter[] cmdParams = null;
    string sSql = "";
    List<CommandInfo> cis = new List<CommandInfo>();
    CommandInfo ci;

    sSql = "update SJLR_bqql set bqql_SFSB=0 where cbsjid=" + e.CommandArgument.ToString();
    ci = new CommandInfo();
    ci.CommandText = sSql;
    ci.Parameters = cmdParams;
    cis.Add(ci);

    sSql = "update SJLR_jgtz_bqql set bqql_SFSB=0 where cbsjid=" + e.CommandArgument.ToString();
    ci = new CommandInfo();
    ci.CommandText = sSql;
    ci.Parameters = cmdParams;
    cis.Add(ci);

    if (DbHelperSQL.ExecuteSqlTran(cis) > 0)
    {
      //ZHCX.CreateZHCX(sKHID, sRQ);
      //ZHCX.UpdateCB(sKHID, sRQ, e.CommandArgument.ToString());
      BindGrid();
    }
  }

  protected void btnQuery_Click(object sender, EventArgs e)
  {

  }

  protected void btnExport_Click(object sender, EventArgs e)
  {
    this.ASPxGridViewExporter1.WriteXlsToResponse();
  }

  protected void cpPopup_Callback(object source, DevExpress.Web.ASPxClasses.CallbackEventArgsBase e)
  {
    litText.Text = CBSJ.GetNotes(e.Parameter).ToString();
  }
}
